<template>
  <div class="login">
    <el-card shadow="hover" class="box-card">
      <div slot="header" class="clearfix">
        <span>注册</span>
        <span v-if="isTrue" style="float: right; color: red">{{
          errorMsg
        }}</span>
      </div>
      <div class="text item">
        <el-form
          :rules="rules"
          label-position="left"
          label-width="80px"
          :model="loginUser"
        >
          <el-form-item label="账号" prop="username">
            <el-input v-model="loginUser.username"></el-input>
          </el-form-item>
          <el-form-item label="昵称" prop="nickname">
            <el-input v-model="loginUser.nickname"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="loginUser.password"></el-input>
          </el-form-item>
          <el-form-item
            type="password"
            label="确认密码"
            @blur.native.capture="confirm"
          >
            <el-input v-model="surePassword"></el-input>
          </el-form-item>
          <el-form-item label="性别" prop="gender">
            <el-radio-group v-model="loginUser.gender">
              <el-radio label="男"></el-radio>
              <el-radio label="女"></el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="所在省份" prop="address">
            <el-select v-model="loginUser.address" placeholder="请选择所在省份">
              <el-option
                v-for="(item, index) in cityList"
                :key="index"
                :label="item"
                :value="item"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-form>
      </div>
      <el-button
        style="float: right; margin-bottom: 20px"
        type="primary"
        @click="submitForm()"
        >注册</el-button
      >
    </el-card>
  </div>
</template>

<script>
import user from "@/api/user";
import { getCity } from "@/utils/city";

export default {
  data() {
    return {
      loginUser: {
        gender: "男",
      },
      errorMsg: "",
      isTrue: false,
      surePassword: "",
      cityList: [],
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            min: 4,
            max: 16,
            message: "长度在 4 到 16 个字符",
            trigger: "blur",
          },
        ],
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
          {
            min: 4,
            max: 16,
            message: "长度在 4 到 16 个字符",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            min: 4,
            max: 16,
            message: "长度在 4 到 16 个字符",
            trigger: "blur",
          },
        ],
        address: [{ required: true, message: "所在省份", trigger: "change" }],
      },
    };
  },

  created() {
    this.cityList = getCity();
  },

  methods: {
    submitForm() {
      user.register(this.loginUser).then((res) => {
        if (res.data.error_msg === "" || res.data.error_msg === undefined) {
          this.isTrue = false;
          this.errorMsg = "";
          this.$message({
            message: "注册成功",
            type: "success",
          });
          this.$router.push({ path: "/login" });
        } else {
          this.errorMsg = res.data.error_msg;
          this.isTrue = true;
        }
      });
    },
    confirm() {
      if (this.surePassword != this.loginUser.password) {
        this.errorMsg = "两次输入的密码不一样";
        this.isTrue = true;
      } else {
        this.errorMsg = "";
        this.isTrue = false;
      }
    },
  },
};
</script>

<style scoped>
.login {
  width: 40%;
  margin-left: 30%;
  margin-right: 30%;
  margin-top: 5%;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}
</style>